package com.hotelvip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotelvip.common.exception.ServiceException;
import com.hotelvip.dao.IdentityMapper;
import com.hotelvip.entity.Identity;
import com.hotelvip.entity.User;
import com.hotelvip.service.IdentityService;
import com.hotelvip.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Author: ypb
 * @Date:2021/1/25 10:41
 */

@Service
public class IdentityServiceImpl extends ServiceImpl<IdentityMapper, Identity> implements IdentityService {

    @Autowired
    private UserService userService;

    /**
     * 查询介绍人身份的id
     *
     * @return 所有的介绍人身份
     */
    @Override
    public List<Identity> getIdentityParent() {
        QueryWrapper<Identity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Identity::getParentId, 0);
        List<Identity> identityList = list(wrapper);
        return identityList;
    }

    /**
     * 通用过介绍人的身份id查询对应的户
     *
     * @param parentId
     * @param key
     * @return 介绍人身份姓名
     */

    @Override
    public List<Identity> findIdentityByParentId(String key, Integer parentId) {
        //通过介绍人的身份id查询到对应的介绍人
        //1封装查询条件
        QueryWrapper<Identity> wrapper = new QueryWrapper<>();
        if (key != null && key != "") {
            wrapper.lambda().like(Identity::getIdentityName, key);
        }
        wrapper.lambda().eq(Identity::getParentId, parentId);
        List<Identity> list = list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list;
    }

    /**
     * @param id
     * @param identityName 需要修改/更改的身份
     * @return
     */
    @Override
    public void insertOrUpdateIdentities(Long id, String identityName) {
        //判断当前身份是否存在
        User user = new User();
        if (id!=null){
           user.setId(id);
        }
        user.setDeleted(0).setVStatus(3).setName(identityName);
            boolean b = userService.saveOrUpdate(user);
        if (!b) {
            throw new ServiceException("新增/修改失败");
        }
    }
}

